package upage

/**
 缺省的page的信息
 */
const (
    PageNoDefault = 0             // 缺省的页码
    PageSizeDefault = 20          // 缺省页条目size
)

type Page struct {
    PageNo     int
    PageSize   int
    TotalPage  int
    TotalCount int
    FirstPage  bool
    LastPage   bool
    List       interface{}
}

func PageUtil(count int, pageNo int, pageSize int, list interface{}) Page {
    tp := count / pageSize
    if count % pageSize > 0 {
        tp = count / pageSize + 1
    }
    return Page{PageNo: pageNo, PageSize: pageSize, TotalPage: tp, TotalCount: count, FirstPage: pageNo == 1, LastPage: pageNo == tp, List: list}
}

/**
 基于传入的值，算出offset和limit
 */
func GetOffsetLimit(pageNo int, pageSize int) (int, int) {
    var offset int

    if pageNo <= 0 {
        offset = 0
    } else {
        offset = (pageNo - 1) * pageSize
    }

    return offset, pageSize
}

